﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;
using Ext.Net;
using FSS.Core.Util;

namespace SalesManagement.Controls.Sales
{
    public partial class UserForm : System.Web.UI.UserControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            BindingStoreRole();
            BindingStoreEmployee();
        }
        private void BindingStoreRole()
        {
            RoleService roleS = RoleService.GetInstance();
            IList<RoleEntity> lstRole = roleS.LoadAll();
            if (null != lstRole)
            {
                this.storeRole.DataSource = lstRole;
                this.storeRole.DataBind();
            }
        }
        private void BindingStoreEmployee()
        {
            EmployeeService employeeS = EmployeeService.GetInstance();
            IList<EmployeeEntity> lstEmployee = employeeS.LoadAll();
            if (null != lstEmployee)
            {
                this.storeEmployee.DataSource = lstEmployee;
                this.storeEmployee.DataBind();
            }
        }
        public void Show()
        {
            this.winDetails.Show();
        }
        public void Show(string username)
        {            
            UserEntity userE = new UserEntity();
            UserService userS = UserService.GetInstance();
            userE.UserName = username;
            userE = userS.LoadByPrimaryKey(userE);
            if (null == userE)
                return;
            hdfUserName.Value = userE.UserName;
            txtUserName.Text = userE.UserName;
            txtUserName.Disabled = true;
            txtPassword.Text = userE.Password;
            txtConfirmPassword.Text = userE.Password;
            cbbRole.SetValue(userE.RoleId);
            cbbEmployee.SetValue(userE.EmployeeId);
            cbbEmployee.Disabled = true;
            ckbIsActive.Checked = userE.IsActive;
            this.winDetails.Show();
        }
        public void ShowAndReset()
        {
            this.FormPanel1.Reset();
            txtUserName.Disabled = false;
            cbbEmployee.Disabled = false;                        
            this.winDetails.Show();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            UserEntity userE = new UserEntity();
            UserService userS = UserService.GetInstance();
            if ("" != hdfUserName.Value.ToString())
            {
                userE.UserName = txtUserName.Text;
                userE = userS.LoadByPrimaryKey(userE);
                userE.Password = Encryption.StringToMD5Hash(txtPassword.Text);
                userE.RoleId = int.Parse(cbbRole.SelectedItem.Value);                
                userE.IsActive = ckbIsActive.Checked;
                userE.UpdatedDate = DateTime.Now;
                userE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                userS.Update(userE);
            }
            else
            {
                userE.UserName = txtUserName.Text;
                userE.Password = Encryption.StringToMD5Hash(txtPassword.Text);
                userE.RoleId = int.Parse(cbbRole.SelectedItem.Value);
                userE.EmployeeId = int.Parse(cbbEmployee.SelectedItem.Value);
                userE.IsActive = ckbIsActive.Checked;
                userE.CreatedDate = DateTime.Now;
                userE.CreatedBy = FSSContext.Current.User.EmployeeId;
                userE.UpdatedDate = DateTime.Now;
                userE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                userS.Insert(userE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}